package com.leetcode.binary.tree;

public class L_108_ConvertSortedArrayToBinarySearchTree {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public TreeNode sortedArrayToBST(int[] nums) {
        if (nums== null || nums.length == 0) {
            return null;
        }
        return constructBinaryTree(0, nums.length - 1, nums);
    }

    public TreeNode constructBinaryTree(int start, int end, int[] nums) {

        if (start > end) {
            return null;
        }

        int mid = (start + end) / 2;
        TreeNode node = new TreeNode(nums[mid]);
        node.left = constructBinaryTree(start, mid - 1, nums);
        node.right = constructBinaryTree(mid + 1, end, nums);
        return node;
    }


}
